package com.github.thinwonton.quartz.misfire;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.SimpleTrigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JDBCStoreTest {
    private static final Logger logger = LoggerFactory.getLogger(JDBCStoreTest.class);

    public static void main(String[] args) throws SchedulerException, InterruptedException {
        StdSchedulerFactory schedulerFactory = new StdSchedulerFactory();
        schedulerFactory.initialize("quartz.properties"); //加载配置文件
        Scheduler scheduler = schedulerFactory.getScheduler();

        logger.info("Scheduler Name:" + scheduler.getSchedulerName());

        // 调度器开始工作
        scheduler.start();

        //创建一个job，并且赋予一些数据
        JobDetail job = JobBuilder.newJob(MyJob.class).withIdentity("job_name_myjob").build();
//        JobDetail job = JobBuilder.newJob(MyJob.class).build();

        // 每隔10秒钟执行一次，共执行5次
        SimpleTrigger trigger = TriggerBuilder.newTrigger().startNow()
                .withSchedule(SimpleScheduleBuilder.repeatSecondlyForTotalCount(5, 10))
                .build();

        // job和trigger注册到scheduler
        scheduler.scheduleJob(job, trigger);

        Thread.sleep(1000);

        logger.info("关闭调度器ing...");
        // 关闭调度器，此时调度器将会经过shutting down -> paused -> shutdown complete三种状态
        scheduler.shutdown();

    }
}
